#include "MyEditor.h"

#include "FMyStyle.h"
#include "LocalizationDescriptor.h"
#include "MyConmmands.h"
#include "SMyButton.h"
#include "Framework/Notifications/NotificationManager.h"
#include "Widgets/Notifications/SNotificationList.h"

#define LOCTEXT_NAMESPACE "MyEditor"

MyEditor::MyEditor(){
	CommandList = MakeShareable(new FUICommandList());

	FMyConmmands::Register();
	
	// 添加命令动作
	CommandList->MapAction(
		FMyConmmands::Get().OpenFileCommand,
		FExecuteAction::CreateLambda([](){
			UE_LOG(LogTemp, Display, TEXT("OpenFile..."));
		}));

	CommandList->MapAction(
		FMyConmmands::Get().CloseFileCommand,
		FExecuteAction::CreateLambda([](){
			UE_LOG(LogTemp, Display, TEXT("CloseFile..."));
		}));

	CommandList->MapAction(
		FMyConmmands::Get().SaveFileCommand,
		FExecuteAction::CreateLambda([](){
			UE_LOG(LogTemp, Display, TEXT("SaveFile..."));
		}));
	
	const TSharedRef<FTabManager::FLayout> Layout = FTabManager::NewLayout(TEXT("Layout"));
	
	TArray<FName> TabName = {"Left", "RightTop", "RightBottom"};

	static FSlateColorBrush YelloBrush = FSlateColorBrush(FLinearColor(1.0f, 1.0f, 0.0f, 1.0f));
	static FSlateColorBrush GreenBrush = FSlateColorBrush(FLinearColor(0.0f, 1.0f, 0.0f, 0.0f));
	static FSlateColorBrush RedBrush = FSlateColorBrush(FLinearColor(1.0f, 0.0f, 0.0f, 0.0f));
	FMyStyle::ResetToDefault();
	FGlobalTabmanager::Get()->RegisterTabSpawner(TabName[0], FOnSpawnTab::CreateLambda(
		[&](const FSpawnTabArgs& Args)->TSharedRef<SDockTab>{
			return SNew(SDockTab)[
				SNew(SVerticalBox)
				+SVerticalBox::Slot()
				.AutoHeight()[
					// 放置菜单
					MakeMenuBar()
				]
				+SVerticalBox::Slot()
				.AutoHeight()[
					// 放置工具栏
					MakeToolBar()
				]
				+SVerticalBox::Slot()
				.FillHeight(1)[
					SNew(SVerticalBox)
						+SVerticalBox::Slot()[
							// fill color
							SNew(SBorder)
								.BorderImage(&YelloBrush)
						]
						+SVerticalBox::Slot()[
							// fill image
							SNew(SBorder)
								.BorderImage(FMyStyle::Get().GetBrush("TrashCan"))
						]
						+SVerticalBox::Slot()[
							// fill nine grid image
							SNew(SBorder)
								.BorderImage( FCoreStyle::Get().GetBrush("Debug.Border"))
						]
				]
			];
		}
	));

	FGlobalTabmanager::Get()->RegisterTabSpawner(TabName[1], FOnSpawnTab::CreateLambda(
		[&](const FSpawnTabArgs& Args)->TSharedRef<SDockTab>{
			return SNew(SDockTab)[
				SNew(SVerticalBox)
				+SVerticalBox::Slot()
				.FillHeight(1)[
					SAssignNew(Editor, SEditableText)
					// SNew(SEditableText)
				]
				+SVerticalBox::Slot()
				.AutoHeight()[
					SNew(SMyButton)
						.Text(FText::FromName(TEXT("获取内容")))
						.OnClicked_Lambda([this](){
							UE_LOG(LogTemp, Display, TEXT("The content text is: %s"), *Editor->GetText().ToString());
							return FReply::Handled();
						})
				]
			];
		}
	));

	FGlobalTabmanager::Get()->RegisterTabSpawner(TabName[2], FOnSpawnTab::CreateLambda(
		[&](const FSpawnTabArgs& Args)->TSharedRef<SDockTab>{
			return SNew(SDockTab)
				.Label(FText::FromString(TEXT("右下面板")))[
					SNew(SSplitter)
					+SSplitter::Slot()
					.Value(2)[
						SNew(SOverlay)
						+SOverlay::Slot()[
							SNew(SBorder)
							.BorderImage(&RedBrush)[
								SNew(STextBlock)
									.Text(FText::FromString("1-Layer"))
							]
						]
						+SOverlay::Slot()[
							SNew(SBorder)
							.BorderImage(&GreenBrush)[
								SNew(STextBlock)
									.Text(FText::FromString("2-Layer"))
							]
						]
						+SOverlay::Slot()[
							SNew(SBorder)
							.BorderImage(&YelloBrush)[
								SNew(STextBlock)
									.Text(FText::FromString("3-Layer"))
							]
						]
					]
					+SSplitter::Slot()
					.Value(1)[
						SNew(SMyButton)
							.Text(FText::FromName(TabName[2]))
							.OnClicked_Lambda([](){
								FNotificationInfo Info(NSLOCTEXT("MainFrame", "RecompileInProgress", "Compile C++ Code"));
								Info.ExpireDuration = 5.0f;
								Info.bFireAndForget = true;
								FSlateNotificationManager::Get().AddNotification(Info);
								return FReply::Handled();
							})
					]
			];
		}
	));
	
	Layout->AddArea(
		FTabManager::NewArea(1024, 768)
			->SetOrientation(Orient_Horizontal)
			->Split(
				FTabManager::NewStack()
				->AddTab(TabName[0], ETabState::OpenedTab)
			)
			->Split(
				FTabManager::NewSplitter()
				->SetOrientation(Orient_Vertical)
				->Split(
					FTabManager::NewStack()
					->AddTab(TabName[1], ETabState::OpenedTab)
				)
				->Split(
					FTabManager::NewStack()
					->AddTab(TabName[2], ETabState::OpenedTab)
				)
			)
	);
	
	FGlobalTabmanager::Get()->RestoreFrom(Layout, TSharedPtr<SWindow>(), true, EOutputCanBeNullptr::IfNoOpenTabValid);
}

TSharedRef<SWidget> MyEditor::MakeMenuBar(){
	FMenuBarBuilder MenuBuilder(CommandList);
	MenuBuilder.AddPullDownMenu(
		LOCTEXT("MyMenu", "文件"),
		LOCTEXT("MyMenu", "文件"),
		FNewMenuDelegate::CreateLambda([](FMenuBuilder& MenuBuilder){
			MenuBuilder.AddMenuEntry(FMyConmmands::Get().OpenFileCommand);
			MenuBuilder.AddMenuEntry(FMyConmmands::Get().CloseFileCommand);
			MenuBuilder.AddMenuEntry(FMyConmmands::Get().SaveFileCommand);
		})
	);
	return MenuBuilder.MakeWidget(); 
}

TSharedRef<SWidget> MyEditor::MakeToolBar(){
	FToolBarBuilder ToolBarBuilder(CommandList, FMultiBoxCustomization::None);
	ToolBarBuilder.BeginSection("MySection");
	ToolBarBuilder.AddToolBarButton(FMyConmmands::Get().OpenFileCommand);
	ToolBarBuilder.AddToolBarButton(FMyConmmands::Get().CloseFileCommand);
	ToolBarBuilder.AddToolBarButton(FMyConmmands::Get().SaveFileCommand);
	ToolBarBuilder.EndSection();
	return ToolBarBuilder.MakeWidget();
}

#undef LOCTEXT_NAMESPACE
